package model

import (
	"bytes"
	"errors"
	"github.com/jmoiron/sqlx"
	"strings"
)

// 测点信息
type Points struct {
	ID           int    `db:"ID"`           //点ID值
	Name         string `db:"Name"`         //测点名
	InternalName string `db:"InternalName"` //测点别名
	Type         int16  `db:"Type"`         //监测类别
	Description  string `db:"Description"`  //备注
	CustomTextN  string `db:"CustomTextN"`  //备注2
}

// 根据【点ID值】查询【测点信息】表中是否存在相关记录
func (d *Points) Exist(ID int) (bool, error) {
	rows, err := amp.Queryx("SELECT COUNT(0) Count FROM points WHERE ID=?", ID)
	if err != nil {
		return false, err
	}
	defer rows.Close()
	count := 0
	if rows.Next() {
		err = rows.Scan(&count)
		if err != nil {
			return false, err
		}
		return count > 0, nil
	}
	return false, nil
}

// 插入单条记录到【测点信息】表中
func (d *Points) Insert() (int64, error) {
	result, err := amp.Exec("INSERT INTO points(Name,InternalName,Type,Description,CustomTextN) VALUES(?,?,?,?,?)", d.Name, d.InternalName, d.Type, d.Description, d.CustomTextN)
	if err != nil {
		return -1, err
	}
	return result.LastInsertId()
}

// 根据【点ID值】修改【测点信息】表的单条记录
func (d *Points) Update() (bool, error) {
	result, err := amp.Exec("UPDATE points SET Name=?, InternalName=?, Type=?, Description=?, CustomTextN=? WHERE ID=?", d.Name, d.InternalName, d.Type, d.Description, d.CustomTextN, d.ID)
	if err != nil {
		return false, err
	}
	affected, err := result.RowsAffected()
	if err != nil {
		return false, err
	}
	return affected > 0, nil
}

// 根据【点ID值】查询【测点信息】表中的单条记录
func (d *Points) Get(ID int) (points Points, err error) {
	rows, err := amp.Queryx("SELECT ID, Name, InternalName, Type, Description, CustomTextN FROM points WHERE ID=?", ID)
	if err != nil {
		return points, err
	}
	defer rows.Close()
	pointss, err := d._RowsToArray(rows)
	if err != nil {
		return points, err
	}
	if len(pointss) <= 0 {
		return points, err
	}
	return pointss[0], nil
}

// 根据【点ID值】数组查询【测点信息】表中的多条记录
func (d *Points) GetIn(IDs []int) (pointss []Points, err error) {
	if len(IDs) <= 0 {
		return pointss, errors.New("IDs is empty")
	}
	sqlStr := bytes.Buffer{}
	sqlStr.WriteString("SELECT ID, Name, InternalName, Type, Description, CustomTextN FROM ")
	sqlStr.WriteString("points")
	sqlStr.WriteString(" WHERE ID IN(")
	paramKeys := strings.Repeat("?,", len(IDs))
	sqlStr.WriteString(paramKeys[:len(paramKeys)-1])
	sqlStr.WriteString(")")
	var rows *sqlx.Rows
	vals := make([]interface{}, 0, len(IDs))
	for _, v := range IDs {
		vals = append(vals, v)
	}
	rows, err = amp.Queryx(sqlStr.String(), vals...)
	if err != nil {
		return pointss, err
	}
	defer rows.Close()
	return d._RowsToArray(rows)
}

// 根据【测点名】查询【测点信息】表中的单条记录，使用索引【uk_points_name,名字唯一】
func (d *Points) GetByName(Name string) (points Points, err error) {
	rows, err := amp.Queryx("SELECT ID, Name, InternalName, Type, Description, CustomTextN FROM points FORCE INDEX(uk_points_name) WHERE Name=?", Name)
	if err != nil {
		return points, err
	}
	defer rows.Close()
	pointss, err := d._RowsToArray(rows)
	if err != nil {
		return points, err
	}
	if len(pointss) <= 0 {
		return points, err
	}
	return pointss[0], nil
}

// 查询【测点信息】表总记录数
func (d *Points) GetRowCount() (count int, err error) {
	rows, err := amp.Queryx("SELECT COUNT(0) Count FROM points")
	if err != nil {
		return -1, err
	}
	defer rows.Close()
	if rows.Next() {
		err = rows.Scan(&count)
		if err != nil {
			return -1, err
		}
		return count, nil
	}
	return -1, nil
}

// 分页查询【测点信息】表的记录
func (d *Points) GetRowList(PageIndex, PageSize int) (pointss []Points, err error) {
	rows, err := amp.Queryx("SELECT ID, Name, InternalName, Type, Description, CustomTextN FROM points LIMIT ?,?", (PageIndex-1)*PageSize, PageSize)
	if err != nil {
		return pointss, err
	}
	defer rows.Close()
	return d._RowsToArray(rows)
}

// 解析【测点信息】表记录
func (d *Points) _RowsToArray(rows *sqlx.Rows) (models []Points, err error) {
	for rows.Next() {
		mo := Points{}
		err = rows.Scan(&mo.ID, &mo.Name, &mo.InternalName, &mo.Type, &mo.Description, &mo.CustomTextN)
		if err != nil {
			return models, err
		}
		models = append(models, mo)
	}
	return models, err
}
